package com.hysoft.web.controller;

import com.hysoft.common.dto.GridParams;
import com.hysoft.common.result.GridResult;
import com.hysoft.common.result.Result;
import com.hysoft.pdr.entity.base.BaseEntity;
import com.hysoft.web.service.IBaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <h1>封装基础的单表增删改查方法</h1>
 * @param <T> 实体类类型
 */
@RestController
public abstract class BaseController<T extends BaseEntity> {

    public abstract IBaseService<T> getBaseService();

    @Operation(summary = "新增或修改")
    @PostMapping("/save")
    public Result<T> save(@Parameter(description = "保存的对象") @RequestBody T entity) {
        return this.getBaseService().insertOrUpdate(entity);
    }

    @Operation(summary = "删除")
    @PostMapping("/delete")
    public Result<List<String>> remove(@Parameter(description = "删除的主键集合") @RequestBody List<String> ids) {
        return this.getBaseService().remove(ids);
    }

    @Operation(summary = "根据主键id获取相应信息")
    @GetMapping("/get/{id}")
    public Object get(@Parameter(description = "主键id") @PathVariable("id") String id) {
        return this.getBaseService().get(id);
    }

    @Operation(summary = "查询")
    @PostMapping("/query")
    public GridResult<?> query(@Parameter(description = "查询表格参数") @RequestBody GridParams<Map<String, Object>> params) {
        return this.getBaseService().query(params);
    }
}
